package dropDownMenu {
	import caurina.transitions.Tweener;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	
	public class DropDownMenuCreator extends MovieClip {
		
		private var _adjustToContent:Boolean;
		
		private var _fillColor:uint = 0x666666;
		private var _lineColor:uint = 0xFFFFFF;
		
		private var _fillAlpha:Number = 1;
		private var _lineAlpha:Number = 1;
		
		private var _boxWidth:Number;
		private var _boxHeight:Number;
		
		private var _roundCorner:Number = 8;
		
		private var _lineOffSet:Number = 10;
		private var _lineThikness:Number = 2;
		
		private var _whiteSpace:int=5;
		
		private var _customContent:MovieClip;
		
		private var _transitionTime:Number=0.2;
		private var _showTime:Number=5000;
		private var _waitTime:Number=0;
		private var _actionTimer:Timer;
		private var _showTimer:Timer;
		
		private var _emiterWidth:Number;
		private var _emiterHeight:Number;
		
		private var _position:String="top";
		
		private var _buttonEmiter:MovieClip;
		private var _container:MovieClip;
		private var _bodyShape:Shape;
		
		private var _maxHeight:Number=400;
		
		private var _tipEnabled:Boolean;
		private var _blockHeight:Boolean;
		
		private static var logger:ILogger = Log.getLogger("DropDownMenuCreator");
		private var _menuCreated:Boolean;
		
		
		public function DropDownMenuCreator(buttonEmiter:MovieClip, customContent:MovieClip, fillColor:uint, lineColor:uint, adjustToContent:Boolean=true, blockHeight:Boolean=true, boxWidth:Number=200, boxHeight:Number=50)
		{	
			logger.log(LogEventLevel.INFO, "DropDownMenuCreator");
			
			_container= new MovieClip;
			_bodyShape=new Shape;
			
			_showTimer= new Timer(_showTime, 1);

			_buttonEmiter=buttonEmiter;
			_customContent=customContent;
			_fillColor=fillColor;
			_lineColor=lineColor;
			_adjustToContent=adjustToContent;
			_blockHeight=blockHeight;
			if (!_adjustToContent) {
				_boxWidth=boxWidth;
				_boxHeight=boxHeight;
			}

			_emiterWidth=buttonEmiter.width;
			_emiterHeight=buttonEmiter.height;
			
			addButtonBehavior();
			createMenu();
			
		}
		
		private function createMenu():void{
			_container.visible=false;
			_container.alpha=0;
			
			if (_adjustToContent){
				_boxWidth=_customContent.width;
				_boxHeight=_customContent.height;
				createBox();
			}else{
				createBox();
			}
			
			_container.addChildAt(_bodyShape, 0);
			_customContent.x=_lineOffSet;
			_customContent.y=_lineOffSet;
			_container.addChild(_customContent);
			setContainerPosition();
			_buttonEmiter.addChild(_container);
		}
		
		private function setContainerPosition():void {
			
			switch(_position) {
				case "top":
					_container.x = _emiterWidth/2-_boxWidth/2;
					_container.y = -_whiteSpace-_boxHeight;
					break;
				case "bottom":
					
					break;
				case "left":
					
					break;
				case "right":
					
					break;
				case "top_left":
					_container.x = _emiterWidth-_emiterWidth;
					_container.y = -_whiteSpace-_boxHeight;
					break;
				default:
					_container.x = _emiterWidth/2-_boxWidth/2;
					_container.y = -_whiteSpace-_boxHeight;
				break;
			}
			
		}
		
		public function showMenu():void {
			if (!checkEmpty()) {
				_container.visible=true;
				Tweener.addTween(_container, {alpha:1, time:_transitionTime, transition:"linear"});
			}
		}
		
		private	function hide(event:TimerEvent):void {
			_showTimer.stop();
			_showTimer.reset();
			fadeOut();
		}
		
		public function forceHide():void {
			removeMenu();
			_showTimer.stop();
			_showTimer.reset();
	
		}
		
		private function fadeOut():void {
			Tweener.addTween(_container, {alpha:0, time:_transitionTime, transition:"linear", onComplete:removeMenu});
		}
		
		private function removeMenu():void {
			_container.visible=false;
		}
		
		private function checkTimeOut():void {
			_showTimer.addEventListener(TimerEvent.TIMER, hide);
			_showTimer.start();
		}
		
		private function startShow(event:MouseEvent):void {
			showMenu();
			checkTimeOut();
			_container.addEventListener(MouseEvent.ROLL_OVER, checkOver);
		}
		
		protected function checkOver(event:MouseEvent=null):void
		{
			_showTimer.stop();
			_showTimer.reset();
			_showTimer.removeEventListener(TimerEvent.TIMER, hide);
			_container.removeEventListener(MouseEvent.ROLL_OVER, checkOver);
			_container.addEventListener(MouseEvent.ROLL_OUT, finishShow);
		}
		
		private function finishShow(event:MouseEvent):void {
			_container.removeEventListener(MouseEvent.ROLL_OUT, finishShow);
			fadeOut();
		}
		
		private function createBox():void{
			_bodyShape.graphics.clear();
			_bodyShape.graphics.lineStyle(_lineThikness, _lineColor, _lineAlpha);
			_bodyShape.graphics.beginFill(_fillColor, _fillAlpha);
			_bodyShape.graphics.drawRoundRect(0,0,_boxWidth+_lineOffSet*2, _boxHeight+_lineOffSet*2, _roundCorner, _roundCorner);
			_bodyShape.graphics.endFill();
		}
		
		private function checkEmpty():Boolean {
			var result:Boolean;
			if (_customContent.height<=0) {
				result=true;
			} else {
				result=false;
			}
			return result;
		}
		
		public function get adjustToContent():Boolean
		{
			return _adjustToContent;
		}
		
		public function set adjustToContent(value:Boolean):void
		{
			_adjustToContent = value;
			createMenu();
		}
		
		public function get fillColor():uint
		{
			return _fillColor;
		}
		
		public function set fillColor(value:uint):void
		{
			_fillColor = value;
			createMenu();
		}
		
		public function get lineColor():uint
		{
			return _lineColor;
		}
		
		public function set lineColor(value:uint):void
		{
			_lineColor = value;
			createMenu();
		}
		
		public function get fillAlpha():Number
		{
			return _fillAlpha;
		}
		
		public function set fillAlpha(value:Number):void
		{
			_fillAlpha = value;
			createMenu();
		}
		
		public function get lineAlpha():Number
		{
			return _lineAlpha;
		}
		
		public function set lineAlpha(value:Number):void
		{
			_lineAlpha = value;
			createMenu();
		}
		
		public function get boxWidth():Number
		{
			return _boxWidth;
		}
		
		public function set boxWidth(value:Number):void
		{
			_boxWidth = value;
			createMenu();
		}
		
		public function get boxHeight():Number
		{
			return _boxHeight;
		}
		
		public function set boxHeight(value:Number):void
		{
			_boxHeight = value;
			createMenu();
		}
		
		public function get lineOffSet():Number
		{
			return _lineOffSet;
		}
		
		public function set lineOffSet(value:Number):void
		{
			_lineOffSet = value;
			createMenu();
		}
		
		public function get lineThikness():Number
		{
			return _lineThikness;
		}
		
		public function set lineThikness(value:Number):void
		{
			_lineThikness = value;
			createMenu();
		}
		
		public function get customContent():MovieClip
		{
			return _customContent;
		}
		
		public function set customContent(value:MovieClip):void
		{
			_customContent = value;
			createMenu();					
		}
		
		public function updateSize(_vW:Number, _vH:Number):void {
			_boxWidth=_vW;
			_boxHeight=_vH;
			if (_blockHeight){
				if (_boxHeight<_maxHeight){
					_adjustToContent=true;
					createMenu();					
				} else {
					_boxHeight=_maxHeight;
					_adjustToContent=false;
					createMenu();					
				}
			}else{
				_adjustToContent=true;
				createMenu();					
			}
		}
		
		public function get showTime():Number
		{
			return _showTime;
		}
		
		public function set showTime(value:Number):void
		{
			_showTime = value;			
		}
		
		public function get waitTime():Number
		{
			return _waitTime;
		}
		
		public function set waitTime(value:Number):void
		{
			_waitTime = value;
		}
		
		public function get transitionTime():Number
		{
			return _transitionTime;
		}
		
		public function set transitionTime(value:Number):void
		{
			_transitionTime = value;
		}
		
		public function get position():String
		{
			return _position;
		}
		
		public function set position(value:String):void
		{
			_position = value;
			createMenu();
		}
		
		public function get tipEnabled():Boolean
		{
			return _tipEnabled;
		}
		
		public function get roundCorner():Number
		{
			return _roundCorner;
		}
		
		public function set roundCorner(value:Number):void
		{
			_roundCorner = value;
		}

		public function get buttonEmiter():MovieClip
		{
			return _buttonEmiter;
		}

		public function set buttonEmiter(value:MovieClip):void
		{
			_buttonEmiter = value;

		}
		
		private function addButtonBehavior():void
		{
			_buttonEmiter.buttonMode=true;
			_buttonEmiter.mouseChildren=true;
			_buttonEmiter.addEventListener(MouseEvent.MOUSE_DOWN, startShow);
		}		

		public function get container():MovieClip
		{
			return _container;
		}

		public function set container(value:MovieClip):void
		{
			_container = value;
		}

		public function get maxHeight():Number
		{
			return _maxHeight;
		}

		public function set maxHeight(value:Number):void
		{
			_maxHeight = value;
		}

		public function get blockHeight():Boolean
		{
			return _blockHeight;
		}

		public function set blockHeight(value:Boolean):void
		{
			_blockHeight = value;
		}


	}
}